$(document).ready(function(){
	var TOCPosition_Left;
	var TOCPosition_Top;
	var TOCPosition_Bottom;
	var navBar_Left;
	var navBarWidth;
	var navBarOffset = 20; //to hide the rounded corners at the top of the nav bar
	var navBarHeight = $('#nav_bar_container').height() - navBarOffset;
	var navBarEffectiveHeight = 70; 
	var closestDiv;
	var placeholder;
	var closestObject;
	var numberOfTocColumns;

	// update variables for TOC position
	TOCPosition_Left = $('#firstCol').offset().left;
	TOCPosition_Top = $('#firstCol').offset().top;
	TOCPosition_Bottom = TOCPosition_Top + $('#firstCol').height();
	// set nav bar left position
	navBar_Left = TOCPosition_Left;
	$('#nav_bar_container').css('left', navBar_Left + 'px');
	// set nav bar width
	navBarWidth = $('#toc_container').width();
	$('#nav_bar_container').width(navBarWidth);
	
	$(window).resize(function(){
		// update variables for TOC position
		TOCPosition_Left = $('#firstCol').offset().left;
		TOCPosition_Top = $('#firstCol').offset().top;
		TOCPosition_Bottom = TOCPosition_Top + $('#firstCol').height();

		// set nav bar left position
		navBar_Left = TOCPosition_Left;
		$('#nav_bar_container').css('left', navBar_Left + 'px');

		// set nav bar width
		navBarWidth = $('#toc_container').width();
		$('#nav_bar_container').width(navBarWidth);

		//find last item in longest TOC column
		numberOfTocColumns = $('.columnContainer').length;
		var maxColHeight = 0;
		var currentColHeight = 0;
		for (var i = 1; i <= numberOfTocColumns; i++){
			currentColHeight = 
			$('.tocColumn' + i + ' p:last-child').position().top + 
			$('.tocColumn' + i + ' p:last-child').height();
			if ( currentColHeight >	maxColHeight ) {
				maxColHeight = currentColHeight;
			};
		};		
		//apply the max height to all the TOC columns
		for (var i = 1; i <= numberOfTocColumns; i++){
			$('.tocColumn' + i).height(maxColHeight);
		};
		//same things with TOC headers
		var maxColHeight = 0;
		var currentColHeight = 0;
		for (var i = 1; i <= numberOfTocColumns; i++){
			currentColHeight = $('.tocColumn' + i + ' .tocHeader a').height() + 5;
			if ( currentColHeight >	maxColHeight ) {
				maxColHeight = currentColHeight;
			};
			console.log($('.tocColumn' + i + ' .tocHeader a').height());
		};		
		//apply the max height to all the TOC columns
		for (var i = 1; i <= numberOfTocColumns; i++){
			$('.tocColumn' + i + ' .tocHeader').height(maxColHeight);
		};
		
	});



	
	$(window).scroll(function(event) {

//		console.log("TOCPosition_Bottom = " + TOCPosition_Bottom);
//		console.log("$(#toc_container).height() = " + $('#toc_container').height());
		
		if (TOCPosition_Bottom < $(window).scrollTop()){
			if(!$("#nav_bar_container").is(':animated')){
				$('#nav_bar_container').animate({top: '-2px'});
			};
		} else {
			if(!$("#nav_bar_container").is(':animated')){
				$('#nav_bar_container').animate({top: '-72px'});
			};
		};
		
//		console.log("$(window).scrollTop() = " + $(window).scrollTop()); //how far the top of the scroll window is from the top of the document
		placeholder = 0;
		$('h1[id*="menu_item_anchor"]').each(function(){
//			console.log("$(this).offset().top = " + $(this).offset().top);
//			console.log("$(this).prop(nodeName) = " + $(this).prop("nodeName"));
//			console.log("$(this).text() = " + $(this).text());
			if ($(this).offset().top < $(window).scrollTop()) {
				if ($(this).offset().top > placeholder){
					closestObject = $(this);
				};
			};
		 });
//		console.log("closestObject = " + $(closestObject).length + " -- " + $(closestObject).text());
		 $('#menu_item').text($(closestObject).text());
		});


}); // closing bracket for $(document).ready(function(){ 